<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="ws.SPWebService"%>
<%@ page import="ws.WebServiceBean"%>
<%@ page import="project.utils.Utils"%>
<%@ page import="project.utils.Parser"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<% 
	SPWebService sp = new SPWebService();
	WebServiceBean ws = sp.getWebServiceBeanPort(); 
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Добавление игроков в команду</title>
<style type="text/css">
body {background-color: #000;}
a:link {color: #F60;text-decoration: none;}
a:visited {color: #F60;text-decoration: none;}
a:hover {text-decoration: none;color: #930;}
a:active {text-decoration: none;color: #930;}
</style>
</head>
<body>
	<form name="addPlayer" method="post" action="playersInTeam.jsp">
		<h1 align="center">
			<font color="#00FF00">Добавление игроков в команду</font>
		</h1>
		<div align="center">
			<select name="player" id="select">
				<%
				String[][] responseArray_players = Utils.toArrays(ws.getAllPlayers());
					ArrayList<HashMap<String, ArrayList<String>>> players = Parser.parse(responseArray_players);
					for (int i = 0; i < players.size(); i++) {
				%>
				<option value="<%=players.get(i).get("id").get(0)%>"><%=players.get(i).get("firstname").get(0) + " "
						+ players.get(i).get("lastname").get(0)%></option>
				<%
					}
				%>
			</select> <input type="submit" name="add" value="Добавить" /> &nbsp; <input
				type="text" name="firstname" value="Имя"> <input type="text"
				name="lastname" value="Фамилия"> <input type="submit"
				name="createAndAdd" value="Добавить" /> <font color="FF6600"
				size=5px> <%
	String[][] responseArray_playersInTeam = Utils.toArrays(ws.getPlayersByTeam(Integer.parseInt((String) session.getAttribute("team_id"))));
 	ArrayList<HashMap<String, ArrayList<String>>> playersInTeam = Parser.parse(responseArray_playersInTeam);
 	for (int i = 0; i < playersInTeam.size(); i++) {
 %><p>
					<%=i + 1%>.
					<%=playersInTeam.get(i).get("firstname").get(0)%>
					<%=playersInTeam.get(i).get("lastname").get(0)%>
					<%
						}
					%>
				</p>
			</font>
		</div>
	</form>
	<!-- Здесь будет проводиться обработка добваления игроков -->
	<%
		if (request.getParameter("add") != null) {
			// добавляем существующего игрока
			int[] data = {
					Integer.parseInt((String) session
							.getAttribute("team_id")),
					Integer.parseInt(request.getParameter("player")) };
			List<Integer> dataInt = new ArrayList<Integer>();
			for (int i=0; i<data.length; i++) dataInt.add(data[i]);
			ws.addPlayerInTeam(dataInt);
		}
		if (request.getParameter("createAndAdd") != null) {
			// создаем нового игрока и добавляем его в команду
			String[] dataAboutPlayer = { (String) session.getAttribute("userId"), request.getParameter("firstname"), request.getParameter("lastname")};
			String[][] responseArray_addPlayer = Utils.toArrays(ws.addPlayer(Utils.toList(dataAboutPlayer)));
			ArrayList<HashMap<String, ArrayList<String>>> player = Parser.parse(responseArray_addPlayer);
			int[] data = { Integer.parseInt((String) session
					.getAttribute("team_id")),Integer.parseInt(player.get(0).get("idEntity").get(0))};
			List<Integer> dataInt = new ArrayList<Integer>();
			for (int i=0; i<data.length; i++) dataInt.add(data[i]);
			ws.addPlayerInTeam(dataInt);
		}
	%>
</body>
</html>